package xuli.dao.impl;

import entity.Product_Pic_Info;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import util.C3P0Util;
import xuli.dao.ProductPicDao;

import java.sql.SQLException;
import java.util.List;

public class ProductPicDaoImpl implements ProductPicDao {
    private QueryRunner queryRunner = new QueryRunner(C3P0Util.getDataSource());

    @Override
    public List<Product_Pic_Info> getProductAllPic(Integer productId) {

        String sql = "select * from product_pic_info where product_id=?";
        List<Product_Pic_Info> productPicInfos = null;
        try {
            productPicInfos = queryRunner.query(sql, new BeanListHandler<>(Product_Pic_Info.class), productId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return productPicInfos;

    }


    @Override
    public Product_Pic_Info findMasterPic(Integer productId) {

        String sql = "select * from product_pic_info where product_id = ? and is_master =1 limit 1";
        Product_Pic_Info productPicInfo = null;
        try {
            productPicInfo = queryRunner.query(sql, new BeanHandler<>(Product_Pic_Info.class), productId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return productPicInfo;

    }


}
